﻿using System;
using System.IO;
using System.Text;

namespace FE.Common
{
    public static class FileHelper
    {
        public static string ReadAllText(string path)
        {
            if (File.Exists(path))
            {
                return File.ReadAllText(path);
            }
            else
            {
                //todo 如果没有，则要先创建文件夹
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                return string.Empty;
            }
        }

        public static FileInfo[] GetFileInfos(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("未配置文件服务器路径,获取文件出错!");
            }

            DirectoryInfo folder = new DirectoryInfo(path);

            return folder.GetFiles();
        }

        public static void CopyFile(string source, string destination)
        {
            if (File.Exists(source))
            {
                File.Copy(source, destination, true);
            }
            else
            {
                throw new Exception($"原始文件不存在,请确认路径:[${source}]");
            }
        }

        public static bool TestPath(string path)
        {
            string fileName = Guid.NewGuid().ToString() + ".test";

            using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                byte[] p = Encoding.UTF8.GetBytes("test test");
                fs.Write(p, 0, p.Length);
            }

            return true;
        }
    }
}